import scrapy

from ScrapyObject.spiders.utils.url_utils import *

'''
scrapy crawl ydbc -o ydbc.json
https://wwie.jixuan011.top/

https://semao201.top/
https://lfav92.cc/
https://qmg155.cc/
https://779724.xyz/
https://chimi43.cc/
https://crkb.life/
https://1dpicd6h.shuiguopai4.xyz/vodtype/55.html
https://diss8.cc/
https://myav6.cc/
https://996624.xyz/

'''


#     item['tags'] = tags
#     item['urls'] = url
#     item['names'] = name
#     item['picture_urls'] = pUrl
#     item['video_urls'] = vUrl
class YdbcSpider(scrapy.Spider):
    name = "ydbc"
    # 前缀
    prefix = 'https://wwie.'
    # 中缀
    website = 'jixuan011'
    # 后缀
    suffix = '.top/'
    allowed_domains = [website + '.top']
    start_urls = [prefix + website + suffix]

    def __init__(self):
        self.i = 0

    def parse(self, response):
        content = get_data(response)
        video_urls = get_video_url_one(content)
        tags = response.xpath("//div[@class='text-secondary']//a/text()").extract()
        if len(video_urls) and len(tags):
            for video_url in video_urls:
                self.i = self.i + 1
                yield get_video_item(id=self.i, tags=tags[0], url=response.url, name='', pUrl='', vUrl=format_url_one(video_url))
        else:
            urls = response.xpath("//a[@class='text-secondary group-hover:text-primary']/@ href").extract()
            picture_urls = response.xpath("//div[@class='thumbnail group']//div//a[@name='tiao']//img/@ data-src").extract()
            names = response.xpath("//div[@class='thumbnail group']//div//a[@name='tiao']//img/@ alt").extract()
            if len(urls) and len(picture_urls) and len(names):
                for i in range(len(picture_urls)):
                    self.i = self.i + 1
                    yield get_video_item(id=self.i, tags='', url=split_joint(self.prefix + self.website + self.suffix, urls[i]), name=names[i], pUrl=picture_urls[i], vUrl="")
        url_list = get_url(content)
        for url in url_list:
            if url.startswith('/') or url.startswith('html'):
                yield scrapy.Request(split_joint(self.prefix + self.website + self.suffix, url), callback=self.parse)
